package com.shr.code_interview;

import java.util.Arrays;
import java.util.Collections;

public class Solution_49 {
	
    public int GetUglyNumber_Solution(int index) {
    	if(index < 1)
    		return 0;
    	int i2 = 0, i3 = 0, i5 = 0;
    	int[] uglyNum = new int[index];
    	uglyNum[0] = 1;
    	for(int i = 1;i<index;i++){
    		uglyNum[i] = Collections.min(Arrays.asList(uglyNum[i2] * 2, uglyNum[i3] * 3, uglyNum[i5] * 5)) ;
    		if(uglyNum[i2] * 2 == uglyNum[i]) i2++;
    		if(uglyNum[i3] * 3 == uglyNum[i]) i3++;
    		if(uglyNum[i5] * 5 == uglyNum[i]) i5++; 
    	}
        return uglyNum[uglyNum.length - 1];
    }
}
